import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/modules/auth'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // ==================== 观众端（移动端）路由 ====================
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('@/views/webapp/Home.vue'),
      meta: { 
        title: '星聚 - 发现精彩活动',
        requiresAuth: false,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/events',
      name: 'EventList',
      component: () => import('@/views/webapp/Event/EventList.vue'),
      meta: { 
        title: '活动列表',
        requiresAuth: false,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/event/:id',
      name: 'EventDetail',
      component: () => import('@/views/webapp/Event/EventDetail.vue'),
      props: true,
      meta: { 
        title: '活动详情',
        requiresAuth: false,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/anchor/:id',
      name: 'AnchorDetail',
      component: () => import('@/views/webapp/Anchor/AnchorDetail.vue'),
      props: true,
      meta: { 
        title: '主播主页',
        requiresAuth: false,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/products',
      name: 'ProductList',
      component: () => import('@/views/webapp/Product/ProductList.vue'),
      meta: { 
        title: '商品商城',
        requiresAuth: false,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/product/:id',
      name: 'ProductDetail',
      component: () => import('@/views/webapp/Product/ProductDetail.vue'),
      props: true,
      meta: { 
        title: '商品详情',
        requiresAuth: false,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/profile',
      name: 'UserProfile',
      component: () => import('@/views/webapp/Profile/UserProfile.vue'),
      meta: { 
        title: '个人中心',
        requiresAuth: true,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/orders',
      name: 'OrderHistory',
      component: () => import('@/views/webapp/Profile/OrderHistory.vue'),
      meta: { 
        title: '我的订单',
        requiresAuth: true,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/favorites',
      name: 'Favorites',
      component: () => import('@/views/webapp/Profile/Favorites.vue'),
      meta: { 
        title: '我的收藏',
        requiresAuth: true,
        layout: 'WebAppLayout'
      }
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/webapp/Auth/Login.vue'),
      meta: { 
        title: '登录',
        requiresAuth: false,
        layout: 'AuthLayout'
      }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/webapp/Auth/Register.vue'),
      meta: { 
        title: '注册',
        requiresAuth: false,
        layout: 'AuthLayout'
      }
    },

    // ==================== 主播端（PC端）路由 ====================
    {
      path: '/admin',
      redirect: '/admin/dashboard'
    },
    {
      path: '/admin/dashboard',
      name: 'AdminDashboard',
      component: () => import('@/views/admin/Dashboard.vue'),
      meta: { 
        title: '主播工作台',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/events',
      name: 'AdminEventList',
      component: () => import('@/views/admin/EventManagement/EventList.vue'),
      meta: { 
        title: '活动管理',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/events/create',
      name: 'AdminEventCreate',
      component: () => import('@/views/admin/EventManagement/EventCreate.vue'),
      meta: { 
        title: '创建活动',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/events/edit/:id',
      name: 'AdminEventEdit',
      component: () => import('@/views/admin/EventManagement/EventEdit.vue'),
      props: true,
      meta: { 
        title: '编辑活动',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/products',
      name: 'AdminProductList',
      component: () => import('@/views/admin/ProductManagement/ProductList.vue'),
      meta: { 
        title: '商品管理',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/products/create',
      name: 'AdminProductCreate',
      component: () => import('@/views/admin/ProductManagement/ProductCreate.vue'),
      meta: { 
        title: '创建商品',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/products/edit/:id',
      name: 'AdminProductEdit',
      component: () => import('@/views/admin/ProductManagement/ProductEdit.vue'),
      props: true,
      meta: { 
        title: '编辑商品',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/orders',
      name: 'AdminOrderList',
      component: () => import('@/views/admin/OrderManagement/OrderList.vue'),
      meta: { 
        title: '订单管理',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/analytics',
      name: 'AdminAnalytics',
      component: () => import('@/views/admin/Analytics/Analytics.vue'),
      meta: { 
        title: '数据统计',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },
    {
      path: '/admin/fans',
      name: 'AdminFans',
      component: () => import('@/views/admin/FanManagement/FanList.vue'),
      meta: { 
        title: '粉丝管理',
        requiresAuth: true,
        requiresAnchor: true,
        layout: 'AdminLayout'
      }
    },

    // ==================== 通用路由 ====================
    {
      path: '/404',
      name: 'NotFound',
      component: () => import('@/views/common/NotFound.vue'),
      meta: { 
        title: '页面未找到',
        layout: 'SimpleLayout'
      }
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404'
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title as string
  }
  
  const authStore = useAuthStore()
  
  // 检查是否需要登录
  if (to.meta.requiresAuth && !authStore.isLoggedIn) {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
    return
  }
  
  // 检查是否需要主播权限
  if (to.meta.requiresAnchor && (!authStore.isLoggedIn || !authStore.isAnchor)) {
    next('/')
    return
  }
  
  // 如果已登录，避免访问登录/注册页
  if ((to.path === '/login' || to.path === '/register') && authStore.isLoggedIn) {
    next(authStore.isAnchor ? '/admin/dashboard' : '/home')
    return
  }
  
  next()
})

export default router